//
//  HCAnswerViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/17.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCAnswerViewController.h"

#import "HCHomeAnswerView.h"
#import "PellTableViewSelect.h"

#import "HCAnswerHomeModel.h"
#import "HCLogViewController.h"
@interface HCAnswerViewController () <HCHomeAnswerViewClicked> {
    HCAnswerHomeModel *homeModel;
    HCHomeAnswerView *answerView;
}

@end

@implementation HCAnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"你问我答";
    [self setRightButtonImage:@"happy_home_addd"];
    
    self.dataRequest.delegate = self;
    
    if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
        HCLogViewController *log = [[HCLogViewController alloc] init];
        
        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
        na.navigationBarHidden = YES;
        [self.navigationController presentViewController:na animated:YES completion:nil];
    }else{
        [self.dataRequest postHC_HomeAnswer:HCGETUSER(@"userid")];
    }
    
    answerView = [[HCHomeAnswerView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height)];
    answerView.delegate = self;
    [self.centerView addSubview:answerView];
    
    // Do any additional setup after loading the view.
}

#pragma mark HCHomeAnswerViewClicked
-(void)answerBtnClicked:(NSIndexPath *)indexPath andInpuString:(NSString *)text {
    
    HCAnswerHomeData *data = homeModel.ask[indexPath.row];
    
    [self.dataRequest postHC_UploadAnswer:HCGETUSER(@"userid") question:data.hc_id answerText:text];
}

-(void)rightBtnAction:(UIButton *)btn {
    [PellTableViewSelect addPellTableViewSelectWithWindowFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 73-10, 0, 150, YBKWinFrame.size.height) selectData:@[@"提问",@"我的回答",@"我的提问"] images:@[@"happy_answer_ask",@"happy_home_mineAsks",@"happy_answer_mineAnswer"] action:^(NSInteger index) {
        if (index == 0) {
            //发布问题
            [self pushChildrenViewController:@"HCAskViewController" parameterObject:nil dataObject:nil];
        }else if (index == 2) {
            //我的回答
            [self pushChildrenViewController:@"HCMineAsksViewController" parameterObject:nil dataObject:nil];
        }else{
            //我的提问
            [self pushChildrenViewController:@"HCMineAnswerViewController" parameterObject:nil dataObject:nil];
        }
    } animated:YES];
}

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    if (aType == HC_HOMEANSWERWWW_TYPE) {
        NSLog(@"我的问答首页数据请求成功:%@",dictionary);
        homeModel = [[HCAnswerHomeModel alloc] initWithDictionary:dictionary error:nil];
        answerView.model = homeModel;
    }else{
        NSLog(@"回答提交数据:%@",dictionary);
        if ([dictionary[@"stat"] integerValue] == 200) {
            [XHToast showBottomWithText:@"回答成功"];
        }else if ([dictionary[@"stat"] integerValue] == 300) {
            [XHToast showBottomWithText:@"不能重复回答哦"];
        }else{
            [XHToast showBottomWithText:@"回答失败"];
        }
    }
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    if (aType == HC_HOMEANSWERWWW_TYPE) {
         NSLog(@"我的问答首页请求失败");
        [XHToast showBottomWithText:@"网络请求失败"];
    }else{
        NSLog(@"回答问题请求失败");
        [XHToast showBottomWithText:@"回答失败"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
